#include <mqueue.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>


int main(int argc, char const *argv[])
{   
    struct  mq_attr attr ;
    attr.mq_maxmsg=10;
    attr.mq_msgsize=128;   
  
    
    //1   创建消息队列
    mqd_t mq = mq_open("/mq_test", O_RDWR | O_CREAT, 0666, &attr); // 队列名称一定要加/
    if(mq==-1)
    {
        perror("mq_open");
        return -1;
    }
    //2   发送消息 
    int ret= mq_send(mq, "hello world", strlen("hello world"), 0);
    if(ret==-1)
    {
        perror("mq_send");
        return -1;
    }

    //3   接收消息
    char buf[128]={0};
    int size= mq_receive(mq, buf, 128, 0);
    if(size==-1)
    {
        perror("mq_receive");
        return -1;
    }
    if(size>0)
    {
        printf("receive msg:%s\n", buf);

    }

    //4   关闭  删除
    mq_close(mq);
   // mq_unlink("/mq_test");
    return 0;
}
